package cn.ffcs.uec.view.component;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uec.view.component.bean.UecArticleListboxBean;
import cn.ffcs.uec.view.component.bean.UecImportantListboxBean;
import cn.ffcs.uec.view.component.bean.UecInnovationListboxBean;
import cn.ffcs.uec.view.manager.LogDownManager;
import cn.ffcs.uec.view.manager.UecArticleManager;
import cn.ffcs.uec.view.manager.UecImportantWorkManager;
import cn.ffcs.uec.view.manager.UecInnovationManager;
import cn.ffcs.uec.view.manager.UecStaffAllManager;
import cn.ffcs.uec.view.model.LogDown;
import cn.ffcs.uec.view.model.UecArticle;
import cn.ffcs.uec.view.model.UecImportantWork;
import cn.ffcs.uec.view.model.UecInnovation;
import cn.ffcs.uec.view.model.UecStaffAll;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
/**
 * @Title:创新成果
 * @author BMP
 * @time 2019-09-05
 */
@Controller
@Scope("prototype")
public class UecArticleListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private StaffAndOutStaff uecStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecArticleListboxBean bean = new UecArticleListboxBean();
	/**
	 * 论文专著
	 */
	private UecArticle uecArticle =new UecArticle();;
	
	private UecStaffAll uecStaffAll = new UecStaffAll();
	//方便人员转换
	private StaffAndOutStaff staffAndOutStaff = new StaffAndOutStaff();
	
	private UecArticleManager uecArticleManager = (UecArticleManager) ApplicationContextUtil
			.getBean("uecArticleManager");
	private UecStaffAllManager uecStaffAllManager = (UecStaffAllManager) ApplicationContextUtil
			.getBean("uecStaffAllManager");
	//下载日志
	private LogDownManager logDownManager = (LogDownManager) ApplicationContextUtil
				.getBean("logDownManager");
	private LogDown logDown=new LogDown();
	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;
    int count=0;
	public UecArticleListboxExt() {
		Executions.createComponents(UecStaffConstants.UEC_ARTICLE_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
		this.addForward(UecStaffConstants.ON_UEC_ARTICLE_QUERY, this,
				UecStaffConstants.ON_UEC_ARTICLE_QUERY_RESPONSE);

		this.addForward(UecStaffConstants.ON_CLEAN_UEC_ARTICLE, this,
				UecStaffConstants.ON_CLEAN_UEC_ARTICLE_RESPONSE);

	}

	/**
	 * 初始化
	 * @throws InterruptedException 
	 * @throws ParseException 
	 */
	public void onCreate() throws InterruptedException, ParseException {
	
		this.binbBean();
	}
	/**
	 * @throws ParseException 
	 * 获取全局变量，绑定数据导页面
	 * @Title: binbBean   
	 * @param: @throws InterruptedException      
	 * @return: void      
	 * @throws
	 */
	private void binbBean() throws InterruptedException, ParseException{
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			if("n".equals(screenName.substring(0,1).toLowerCase())) {
				screenName=screenName.substring(1);
			}
			uecStaffAll.setCode(screenName.toUpperCase());
			uecStaffAll = uecStaffAllManager.findUecStaffAll(uecStaffAll);
		}
		if (null == uecStaffAll) {
			Messagebox.show("人员信息获取失败！");
			return;
		}else {
			staffAndOutStaff.setPkPsndoc(uecStaffAll.getPkpsndoc());
			uecArticle.setPkPsndoc(uecStaffAll.getPkpsndoc());
			logDown.setPkPsndoc(uecStaffAll.getPkpsndoc());
			getLogDown(logDown);
			
		}
	}
	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecArticleList() throws Exception {
		if (this.bean.getUecArticleListbox().getSelectedCount() > 0) {
			uecArticle = (UecArticle) bean.getUecArticleListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * ，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecArticleQueryResponse(ForwardEvent event) {
		String talentSwitch = ApprovalUtil.getTalentSwitch();
		if("open".equals(talentSwitch)) {
			this.setButtonValid(false, true,true,true);
			if("N".equals(logDown.getOpenswitch())) {
				this.setButtonValid(false, true,true,true);
			}else {
			  this.setButtonValid(false, false, false, false);	
			}
		}else {
			this.setButtonValid(false, false, false, false);	
		}
			this.bean.getUecArticleListboxPaging().setActivePage(0);
			this.onQueryUecArticleList();
	}
	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecArticleResponse(ForwardEvent event) {
		
		ListboxUtils.clearListbox(this.bean.getUecArticleListbox());
		this.bean.getUecArticleListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd,
			boolean canEdit, boolean candel) {
		//this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecArticle() {
		this.bean.getUecArticleListboxPaging().setActivePage(0);
		this.onQueryUecArticleList();
		
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecArticle() {
		ListboxUtils.clearListbox(this.bean.getUecArticleListbox());
		this.bean.getUecArticleListboxPaging().setTotalSize(1);
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecArticle() throws Exception {
		if("N".equals(getopenSwitch())) {
			if (uecStaffAll != null) {
				// if(count<5) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("opType", "add");
					map.put("uecStaffAll", uecStaffAll);
					Window window = (Window) Executions.createComponents(
							UecStaffConstants.UEC_ARTICLE_EDIT_ZUL, this, map);
					window.doModal();
					window.addEventListener("onOK", new EventListener() {
						@Override
						public void onEvent(Event event) throws Exception {
							if (event.getData() != null) {
								// 新增或修改完成后，列表从数据库更新
								uecArticle = (UecArticle) event.getData();
								onQueryUecArticleList();
							}
						}
					});
					
				/* }else {
					 Messagebox.show("您已维护5条记录,不可以再次新增"); 
				 }*/
			}
		}else {
			Messagebox.show("您已报名,不可以进行信息维护");
			return;
		}
	}
	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecArticle() throws Exception, InterruptedException {
		if("N".equals(getopenSwitch())) {
		if (uecStaffAll != null && this.uecArticle != null) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("opType", "edit");
				map.put("uecArticle", uecArticle);
				map.put("uecStaffAll", uecStaffAll);
				Window window = (Window) Executions.createComponents(
						UecStaffConstants.UEC_ARTICLE_EDIT_ZUL, this, map);
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecArticle = (UecArticle) event.getData();
							onQueryUecArticleList();
						}
					}
				});
		} else {
			ZkUtil.showError("请选择要编辑的记录", "提示信息");
			return;
		}
	   }else {
			Messagebox.show("您已报名,不可以进行信息维护");
			return;
		}	
	}

	/**
	 * 删除按钮
	 * @throws InterruptedException 
	 * @throws ParseException 
	 */
	public void onDelUecArticle() throws ParseException, InterruptedException {
		if("N".equals(getopenSwitch())) {
		if (uecStaffAll != null && uecArticle.getId() != null) {
				ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
					public void onEvent(Event event) throws Exception {
						Integer result = (Integer) event.getData();
						if (result == Messagebox.OK) {
							uecArticle.setStatuscd("1100");
							uecArticle.updateOnly();
							onQueryUecArticleList();
							//PubUtil.reDisplayListbox(bean.getUecArticleListbox(),
							//		uecArticle, "del");
							uecArticle = null;
						}
					}
				});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}else {
		Messagebox.show("您已报名,不可以进行信息维护");
		return;
	}
	}

	/**
	 * 翻页
	 */
	public void onUecArticleListboxPaging() {
		this.onQueryUecArticleList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecArticle = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecArticleList() {
		if (null != uecArticle) {
			PageInfo pageInfo = uecArticleManager.queryPageInfoByUecArticle(uecArticle,
					this.bean.getUecArticleListboxPaging().getActivePage() + 1,
					this.bean.getUecArticleListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecArticleListbox().setModel(list);
			this.bean.getUecArticleListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
			count=pageInfo.getTotalCount();
		}
	}
	 public boolean getLogDown(LogDown logdown) throws ParseException {
		 boolean flag=false;
		 LogDown findLogDownByLogDown = logDownManager.findLogDownByLogDown(logdown);
		 if(findLogDownByLogDown!=null) {
			 flag=true;
			 logDown=findLogDownByLogDown;
		 }
		 return flag;
	 }
	 
	 public String getopenSwitch() throws ParseException {
		 if(getLogDown(logDown)) {
			return logDown.getOpenswitch();
		 }
		 return "N";
	 }
}
